package shuishijie.suishijie.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import shuishijie.suishijie.R;


public class ViewSettingItem extends LinearLayout {

    private final Context mContext;
    private ImageView img;
    private ImageView direction;
    private ImageView src;
    private TextView text;
    private TextView text2;
    private View line;
    private TextView infoCount;

    public ViewSettingItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_setting_item, this);
        initViews(attrs);
    }

    private void initViews(AttributeSet attrs) {
        img = (ImageView) this.findViewById(R.id.img);
        direction = (ImageView) this.findViewById(R.id.direction);
        src = (ImageView) this.findViewById(R.id.src);
        text = (TextView) this.findViewById(R.id.text);
        text2 = (TextView) this.findViewById(R.id.text2);
        line = this.findViewById(R.id.line);
        infoCount = (TextView) this.findViewById(R.id.info_count);

        TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.custom);

        if (img != null) {
            int icon = t.getResourceId(R.styleable.custom_icon, 0);
            if (icon != 0) {
                img.setImageResource(icon);
            } else {
                img.setVisibility(GONE);
            }
        }

        String str = t.getString(R.styleable.custom_text);
        if (TextUtils.isEmpty(str)) {
            text.setVisibility(GONE);
        } else {
            text.setText(str);
        }
        String str2 = t.getString(R.styleable.custom_text2);
        if (TextUtils.isEmpty(str2)) {
            text2.setText("");
        } else {
            text2.setText(str2);
        }

        boolean showLine = t.getBoolean(R.styleable.custom_showUnderLine, true);
        line.setVisibility(showLine ? VISIBLE : GONE);

        boolean d = t.getBoolean(R.styleable.custom_showDirection, true);
        direction.setVisibility(d ? VISIBLE : INVISIBLE);
    }

    public void setText2(String text2) {
        this.text2.setText(text2);
    }

    public void setText2Color(int color) {
        this.text2.setTextColor(color);
    }

    public void setInfoCount(int count) {
        if (infoCount != null) {
            infoCount.setText(count);
            infoCount.setVisibility(VISIBLE);
        }
    }

    public ImageView getSrc() {
        return src;
    }

    public void setText(String text) {
        this.text.setText(text);
    }
}